package pl.asoft.nbp.util;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import pl.asoft.nbp.model.ExchangeRate;
import pl.asoft.nbp.validator.ExchangeRateValidator;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static pl.asoft.nbp.validator.ExchangeRateValidator.validateDates;

/**
 * Created by rbiedrawa on 2014-12-01.
 */
public class DateConverter {

	private static final Logger LOG = Logger.getLogger(DateConverter.class);

    /**
     *Converts date to valid format for links to NBP sites.
     * @param startDate start date
     * @param endDate end date
     * @return List of valid date format.
     * @throws IllegalArgumentException - when date is not valid
     */
	public static List<String> convertDate(Date startDate, Date endDate) throws IllegalArgumentException{

        validateDates(startDate, endDate);
		DateTime startDateTime = new DateTime(startDate);
		DateTime endDateTime = new DateTime(endDate);

        List<String> days = new ArrayList<>();
		while (startDateTime.isBefore(endDateTime)) {
			days.add(ExchangeRateValidator.DATE_FORMAT.print(startDateTime).replaceAll("-", ""));
			startDateTime = startDateTime.plusDays(1);
		}
		days.add(ExchangeRateValidator.DATE_FORMAT.print(endDateTime).replaceAll("-", ""));

		LOG.debug("Requested days size. Duration between two dates (Included endDate date in calculation): "
                + days.size() + " days.");

		return days;
	}


}
